<template>
  <div>
    <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
      <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad">
        <lazy-component class="box_fh">
          <van-cell v-for="item in goodsList" :key="item.id" @click="gotoInfo(item.id)">
            <van-image :src="item.img_url" @error="show_default" />
            <p>{{item.title}}</p>
            <div data-v-144cc9f1 class="bottom_box">
              <div data-v-144cc9f1>
                <span data-v-144cc9f1>￥{{item.sell_price}}</span>
                <s data-v-144cc9f1>￥{{item.market_price}}</s>
              </div>
              <div data-v-144cc9f1>
                <span data-v-144cc9f1>热卖中</span>
                <span data-v-144cc9f1>{{item.stock_quantity}}</span>
              </div>
            </div>
          </van-cell>
        </lazy-component>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 商品列表数据
      list: [],
      goodsList: [],
      pageIndex: 3,
      loading: false,
      finished: false,
      refreshing: false
    };
  },
  methods: {
    async onLoad() {
      // 异步更新数据
      if (this.refreshing) {
        this.goodsList = [];
        this.refreshing = false;
      }

      const { data: res } = await this.$http.get(`api/getgoods`, {
        pageindex: this.pageIndex
      });
      if (res.status !== 0)
        return this.$notify({ type: "danger", message: "获取商品信息失败" });
      this.goodsList = this.goodsList.concat(res.message);

      // 加载状态结束
      this.loading = false;

      // 数据全部加载完成
      if (this.goodsList.length >= 10) {
        this.finished = true;
      }
    },
    onRefresh() {
      setTimeout(() => {
        // 清空列表数据
        this.finished = false;

        // 重新加载数据
        // 将 loading 设置为 true，表示处于加载状态
        this.loading = true;
        this.onLoad();
      }, 1000);
    },
    // 跳转到商品详情页面
    gotoInfo(id) {
      this.$router.push(`/goodsInfo?goodsId=${id}`);
      this.$store.commit('changeCommentId', id);
    },
    show_default(event) {
      event.target.src = "http://temp.im/600x600";
    }
  }
};
</script>

<style lang="less" scoped>
.van-list {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -ms-flex-wrap: wrap;
  flex-wrap: wrap;
  -webkit-box-pack: justify;
  -ms-flex-pack: justify;
  justify-content: space-between;
  margin: 46px 10px 5px;
}

.van-cell {
  position: relative;
  display: -webkit-box;
  display: -webkit-flex;
  display: flex;
  box-sizing: border-box;
  width: 100%;
  padding: 10px 15px;
  overflow: hidden;
  color: #323233;
  font-size: 14px;
  line-height: 24px;
  background-color: #fff;
}

.van-list .van-cell {
  width: 48%;
  border: 1px solid #ccc;
  margin: 10px 0;
  padding: 0;
  -ms-flex-line-pack: justify;
  align-content: space-between;
}

.van-cell__value {
  position: relative;
  overflow: hidden;
  color: #969799;
  text-align: right;
  vertical-align: middle;
}

.van-list .van-cell .van-cell__value--alone .van-image {
  padding: 10px;
  min-height: 180px;
}

.van-image {
  position: relative;
  display: inline-block;
  margin-bottom: 0;
}

.van-list .van-cell .van-cell__value--alone .van-image[data-v-144cc9f1] {
  padding: 10px;
  min-height: 180px;
}

.van-list .van-cell .van-cell__value > p {
  font-size: 14px;
  color: #000;
  text-align: left;
  text-overflow: -o-ellipsis-lastline;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  line-clamp: 2;
  -webkit-box-orient: vertical;
  padding: 4px 10px;
  height: 49px;
}

.van-list .van-cell .van-cell__value--alone {
  text-align: center;
  padding-top: 20px;
}

.van-cell__value {
  position: relative;
  overflow: hidden;
  color: #969799;
  text-align: left;
  vertical-align: middle;
}

.van-list .van-cell .bottom_box {
  width: 100%;
  background: #eee;
  padding: 5px 0;
}

.van-list .van-cell .bottom_box div:first-child {
  text-align: left;
  padding: 0 10px;
}

.van-list .van-cell .bottom_box div:first-child span:first-child {
  font-size: 16px;
  color: red;
  margin-right: 15px;
}

.van-list .van-cell .bottom_box div:nth-child(2) {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-pack: justify;
  -ms-flex-pack: justify;
  justify-content: space-between;
  padding: 0 10px;
  color: #000;
}

.van-list__finished-text,
.van-list__loading {
  width: 100%;
}

.van-list__error-text,
.van-list__finished-text,
.van-list__loading {
  color: #969799;
  font-size: 13px;
  line-height: 50px;
  text-align: center;
}
.box_fh {
  display: flex !important;
  justify-content: space-between !important;
  flex-wrap: wrap;
}
</style>